/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 52461
 */
@ControllerBind(controllerKey = "/view/zk/zk_manager")
public class ZkManagerCtrl extends Controller {
    
    public void index() {
        render("/view/zk/zk_manager.jsp");
    }
    
    public void getDeviceZkItem() {
        
    }
    
    public void getDeviceZkValue() {
        String ZK_REAGENT_PK = getPara("ZK_REAGENT_PK");
        String device = getPara("device");
        String date = getPara("date");
        if (date != null && date.length() == 6) {
            date = date.split("-")[0] + "-0" + date.split("-")[1];
        }
        System.out.println(date);
        String testId = getPara("TESTID");
        String sql = "select a.*,b.SD,b.CV,b.STANDARD_VALUE from zk_test_result a , zk_standard b  where a.ZK_REAGENT_PK=b.ZK_REAGENT_PK and a.DEVICEID=b.DEVICEID AND a.TESTID=b.TESTID and date_format(ZK_DATE,'%Y-%m')='" + date + "' and a.DEVICEID='" + device + "' and a.ZK_REAGENT_PK='" + ZK_REAGENT_PK + "' AND a.TESTID='" + testId + "'";
        System.out.println(sql);
        renderJson(Db.find(sql));
        
    }
    
    public void getAllItems() {
        
        String date = getPara("date");
        String device = getPara("device");
        String ZK_REAGENT_PK = getPara("ZK_REAGENT_PK");
        
        String sql = "select a.*,b.SD,b.CV,b.STANDARD_VALUE from zk_test_result a , zk_standard b  where a.ZK_REAGENT_PK=b.ZK_REAGENT_PK and a.DEVICEID=b.DEVICEID AND a.TESTID=b.TESTID and ZK_DATE='" + date + "' and a.DEVICEID='" + device + "' and a.ZK_REAGENT_PK='" + ZK_REAGENT_PK + "'";
        System.out.println(sql);
        renderJson(Db.find(sql));
        
    }
    
    public void saveItem() {
        List<Map<String, String>> items = new Gson().fromJson(getPara("data"), new TypeToken<List<Map<String, String>>>() {
        }.getType());
        if (items != null) {
            for (Map<String, String> item : items) {
                Db.update("update zk_test_result set RESULT = '" + item.get("RESULT") + "' WHERE PK= " + item.get("PK"));
            }
            
        }
        renderText("true");
        
    }
    
}
